package com.sailing.xjpb.common.web;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sailing.xjpb.common.JsonUtils;
import com.sailing.xjpb.common.exception.BusinessException;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * web工具类-与request和response的工具类
 *
 * @author YW
 */
public class WebUtils {

    private WebUtils() {

    }

    private static final String UNKNOWN_IP = "unknown";

    private static final String COMMA = ",";

    /**
     * getIpAddress[解析ip地址] 创建人: LIULEI 创建时间: Jul 25, 2017 4:49:03 PM
     *
     * @param request 当前请求
     * @since CodingExample Ver(编码范例查看) 1.1
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN_IP.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 如果是多级代理，那么取第一个ip为客户ip
        if (ip != null && ip.contains(COMMA)) {
            ip = ip.substring(0, ip.indexOf(COMMA)).trim();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

    /**
     * 判断当前请求是否是ajax请求
     *
     * @param request 当前请求
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String requestedWith = request.getHeader("x-requested-with");
        return requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest");
    }

    /**
     * 从请求对象中获取请求参数内容 add by YaoWei since 2023-04-11
     *
     * @param request 请求对象
     * @return 请求体内容
     */
    public static StringBuilder getRequestBody(HttpServletRequest request) {
        try (InputStreamReader isr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
             BufferedReader sr = new BufferedReader(isr)) {
            StringBuilder requestBody = new StringBuilder();
            String inputStr;
            while ((inputStr = sr.readLine()) != null) {
                requestBody.append(inputStr);
            }
            return requestBody;
        } catch (IOException e) {
            throw BusinessException.businessError("获取请求体内容失败！", e);
        }
    }

    /**
     * 从请求对象中获取请求参数内容 add by YaoWei since 2023-04-11
     *
     * @param request 请求对象
     * @return 请求体内容
     */
    public static ObjectNode getJsonRequestBody(HttpServletRequest request) {
        return JsonUtils.parseObject(getRequestBody(request).toString());
    }
}
